package com.czd.support.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.czd.base.model.Paginate;
import com.czd.support.model.Manager;
import com.czd.support.model.ZcAnswers;
import com.czd.support.model.ZcAnswersQuestion;
import com.czd.support.model.ZcQuestion;
import com.czd.support.service.ZcAnswersQuestionService;
import com.czd.support.service.ZcAnswersService;
import com.czd.support.service.ZcQuestionService;

@Controller
@RequestMapping("zcAnswers")
public class ZcAnswersController   extends  BaseController  {
	@Autowired
	private ZcAnswersService  zcAnswersService;
	@Autowired
	private  ZcQuestionService  zcQuestionService  ;
	@Autowired
	private  ZcAnswersQuestionService  zcAnswersQuestionService ;
	@RequestMapping(value = { "", "/", "/list" })
	public String list(Model model, ZcAnswers zcAnswers,@RequestParam(value = "pageNo", defaultValue = "1") int pageNo) {
		zcAnswers.setPageNo(pageNo);  
		Paginate queryForPaginate = zcAnswersService.queryForPaginate(zcAnswers);
		model.addAttribute("paginate", queryForPaginate);
		return "zcAnswers/list";
	}
	
	
	/**
	 * w问题回答   调转到 增加页面
	 */
    @RequestMapping(value = "/{id}/add",method = RequestMethod.GET)
    public String change(@PathVariable int id,Model model){
        
    	//根据id查出问题回显给前端
    	  ZcQuestion findById = zcQuestionService.findById(id);
    	model.addAttribute("zcAnswers", findById);
        return "zcAnswers/add";
    }  
    /**
     * w问题回答     增加数据
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String change( HttpServletRequest  httpServletRequest  ,Model model){
    	String qId = httpServletRequest.getParameter("id");
    	String answersContent = httpServletRequest.getParameter("answersContent");
    	
    	//获得用户的id
    	Manager sesionUser = this.getSesionUser(httpServletRequest);
    	int userCode   =0 ;
    	if(sesionUser!=null){
    		 userCode = sesionUser.getUserCode();
    	}
    	//增加答案表  
    	ZcAnswers  zcAnswers   =  new  ZcAnswers()  ;
    	zcAnswers.setCreater(userCode);
    	zcAnswers.setAnswersContent(answersContent);
    	zcAnswersService.addAnswer(zcAnswers);
    	
    	   
    	//查出答案内容id  aId
    	List<ZcAnswers> findByContent = zcAnswersService.findByContent(answersContent);
    	//获取前端传过来数据  增加临时表
    	int aId = findByContent.get(0).getId();
    	ZcAnswersQuestion  zcAnswersQuestion  =  new  ZcAnswersQuestion();
    	zcAnswersQuestion.setaId(aId);
    	zcAnswersQuestion.setqId(Integer.parseInt(qId));
    	zcAnswersQuestionService.addXX(zcAnswersQuestion);
    	
    	//更新标为1
    	zcQuestionService.updateFlag(Integer.parseInt(qId));
    	
    	//返回给问题列表   
    	return "redirect:/zcQuestion/list";
    }
    
    
    
	

}
